package ro.ubbcluj.cs.bibliospringee.rsclient;

import com.google.gson.Gson;
import ro.ubbcluj.cs.bibliospringee.common.dto.AuthorDto;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class RSClient
{
    public static final String FAILED_HTTP_ERROR_CODE_TEXT = "Failed : HTTP error code : ";
    public static final String SERVICE_URL = "http://localhost:8080/bibliospringee-api/rest/author/";

    public static void main(String... args)
    {
        try
        {
            System.out.println(" Getting ALL Authors!");
            getAll();
            System.out.println(" Get Ends! \n");

            System.out.println(" Posting new Author!");
            post();
            System.out.println(" Posting Ends! \n");

            System.out.println(" Getting ALL Authors!");
            getAll();
            System.out.println(" Get Ends! \n");

            System.out.println(" Deleting Author!");
            delete(1);
            System.out.println(" Deleting Ends! \n");

            System.out.println(" Getting ALL Authors!");
            getAll();
            System.out.println(" Get Ends! \n");
        }
        catch (final IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void delete(long id) throws IOException
    {
        final HttpURLConnection connection = initURLConnection("DELETE", Long.toString(id));

        validateResponseAndDisconnect(connection);
    }

    private static void post() throws IOException
    {
        final HttpURLConnection connection = initURLConnection("POST");

        final AuthorDto authorDto = new AuthorDto();
        authorDto.setFirstName("Karoly");
        authorDto.setLastName("Simon");

        final String input = new Gson().toJson(authorDto);

        final OutputStream outputStream = connection.getOutputStream();
        outputStream.write(input.getBytes());
        outputStream.flush();

        validateResponseAndDisconnect(connection);
    }

    private static void validateResponseAndDisconnect(HttpURLConnection connection) throws IOException
    {
        final int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK)
        {
            throw new RuntimeException(FAILED_HTTP_ERROR_CODE_TEXT + responseCode);
        }

        connection.disconnect();
    }

    private static void getAll() throws IOException
    {
        final HttpURLConnection connection = initURLConnection("GET");

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
        {
            throw new RuntimeException(FAILED_HTTP_ERROR_CODE_TEXT + connection.getResponseCode());
        }

        final BufferedReader br = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null)
        {
            System.out.println(output);
        }

        connection.disconnect();
    }

    private static HttpURLConnection initURLConnection(String requestMethod) throws IOException
    {
        return initURLConnection(requestMethod, "");
    }

    private static HttpURLConnection initURLConnection(String requestMethod, String parameter) throws IOException
    {
        final URL url = new URL(SERVICE_URL + parameter);

        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod(requestMethod);
        connection.setRequestProperty("Content-Type", "application/json");

        return connection;
    }
}


